package com.feishi.core.analytic.element;

import com.feishi.core.util.common.bean.ByteBufferProxy;
import com.feishi.core.analytic.parser.PoParser;
/**
 * Created by caixq on 5/4 004.
 */
public class AnalyticStaticInputDataElement<T> extends AnalyticStaticDataElement<T> {

    public AnalyticStaticInputDataElement(int length) {
        init(name,type,length,parser);
    }
    public AnalyticStaticInputDataElement(int type, int length) {
        init(String.valueOf(type),type,length,parser);
    }
    public AnalyticStaticInputDataElement(int type, int length, PoParser<T> parser) {
        init(String.valueOf(type),type,length,parser);
    }

    public AnalyticStaticInputDataElement(String name, int length) {
        init(name,type,length,parser);
    }
    public AnalyticStaticInputDataElement(String name, int length, PoParser<T> parser) {
        init(name,type,length,parser);
    }
    public AnalyticStaticInputDataElement(String name, int type, int length, PoParser<T> parser) {
        init(name,type,length,parser);
    }
    public AnalyticStaticInputDataElement(String name, int type, int length) {
        init(name,type,length,parser);
    }
    public void init(String name,int type, int length, PoParser<T> parser) {
        this.name=name;
        this.type=type;
        this.length=length;
        this.parser=parser;
    }

    public PoParser getParser() {
        return parser;
    }

    public Object getValue(ByteBufferProxy bfp) {
        byte[] bytes = bfp.get(index, length);


        return getValue(bytes);
    }

    public byte[] getBytes(Object o) {
        byte[] bytes=super.getBytes(o);
        bytes= fixToLength(bytes);

        return bytes;
    }

    private byte[] fixToLength(byte[] bytes) {
        byte[] result=new byte[length];
        int maxLength=length>bytes.length?bytes.length:length;


        for(int i=1;i<=maxLength;i++){
            result[result.length-i]=bytes[bytes.length-i];
        }
        return result;
    }


}
